package Day14;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class SavePanel extends JPanel implements ActionListener {
    private JLabel saveLabel = new JLabel("金额");
    private JTextField saveField = new JFormattedTextField();
    private JButton sureBtn = new JButton(new ImageIcon("Image/buttonSure.jpg"));
    private JButton cancelBtn = new JButton(new ImageIcon("Image/buttonOff.jpg"));
    private Font font = new Font("宋体",Font.BOLD,20);
    private ATM05 atm05;
    public SavePanel(ATM05 atm05){
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        saveLabel.setBounds(250,200,80,50);
        saveLabel.setFont(font);
        this.add(saveLabel);

        saveField.setBounds(380,200,250,50);
        saveField.setFont(font);
        this.add(saveField);

        sureBtn.setBounds(370,320,140,50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        cancelBtn.setBounds(520,320,140,50);
        cancelBtn.addActionListener(this);
        this.add(cancelBtn);

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        ArrayList<User> users = atm05.getUsers();
        if (e.getSource() == sureBtn) {
            try {
                double saveMoney = Double.parseDouble(saveField.getText());
                atm05.getLoginUser().setBlance(atm05.getLoginUser().getBlance() + saveMoney);
            }catch (NumberFormatException e1){
                JOptionPane.showMessageDialog(this,"必须输入数字");
                saveField.setText("");
                return;
            }
            JOptionPane.showMessageDialog(this, "存款成功");
            fileUtil.writeObject(users, atm05.getFILE_PATH());
            atm05.showPanel("menu");
            saveField.setText("");
        }else if(e.getSource() == cancelBtn){
            saveField.setText("");
            atm05.showPanel("menu");
        }
    }
}
